Title: Delete Expired Coupons Automatically

Publish Date: Wed, 01 Aug 2018 07:00:00 +0000

Categories: Uncategorized

Content:

Update 30 October 2023: Script tested and confirmed working on WooCommerce 8.2.1.



Coupons are amazing, who does not like a discount? A very common way to give coupons to customers is to create one automatically after their first order, or something like that.



The issue with that though is that usually these coupons expire after a specific period, and by default, WooCommerce will not delete them. They have no reason to stay in your database though, so how do we delete expired coupons automatically?







With a snippet of course.





Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:




https://gist.github.com/SiR-DanieL/e02ca1769c1100de7b908e9ee106a34d




This snippet will automatically move to the Trash all the expired coupons in your store so you can go and delete them permanently with one click, regardless if they have been used or not.



If you want to permanently delete the coupons directly instead of trashing them, you can change this code:



wp_trash_post( $coupon->ID );




to this:



wp_delete_post( $coupon->ID, true );




but beware that once you permanently delete a coupon it cannot be recovered.



Note that it works on WordPress cron jobs. If you disabled them or for any reason, they are not working, this snippet will not work as well.



It will also add a Delete Expired Coupons button in Marketing > Coupons. Beware though, this button might run into timeout issues if you have too many coupons to delete!
